﻿/*
 * Creado por SharpDevelop.
 * Usuario: ian.schofield
 * Fecha: 23/08/2010
 * Hora: 11:49
 * 
 * Para cambiar esta plantilla use Herramientas | Opciones | Codificación | Editar Encabezados Estándar
 */
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Windows.Forms;

namespace RecursivelyDeleteCVS
{
	/// <summary>
	/// Description of MainForm.
	/// </summary>
	public partial class MainForm : Form
	{
		private FolderBrowserDialog theFolderBrowser;
		
		public MainForm()
		{
			//
			// The InitializeComponent() call is required for Windows Forms designer support.
			//
			InitializeComponent();
			
			//
			// TODO: Add constructor code after the InitializeComponent() call.
			//
			this.theFolderBrowser = new System.Windows.Forms.FolderBrowserDialog();
		    this.theFolderBrowser.Description = "Please select a folder for removing all CVS sub-folders.";
    		this.theFolderBrowser.ShowNewFolderButton = false;
    		this.theFolderBrowser.RootFolder = System.Environment.SpecialFolder.Desktop;
    		//this.theFolderBrowser.SelectedPath = "C:\\Documents and Settings\\ian.schofield.NET2UC0122\\Mis documentos\\Proyectos\\AST\\TDTConsola";
 		}
		
		void BtnGoClick(object sender, EventArgs e)
		{
			try {
				if (this.theFolderBrowser.ShowDialog() == DialogResult.OK) {
			        recurse(theFolderBrowser.SelectedPath);
			        this.lblRuta.Text = "Finished deleting!";
				} else {
					return;	
				}
			} catch(Exception exp) {
                MessageBox.Show("An error occurred while executing. The error is:" 
                                + System.Environment.NewLine + exp.ToString() + System.Environment.NewLine);
				this.lblRuta.Text = "Error!";
            }
		}
		
		// Recursively go through all folders, deleting all 'CVS' folders 
		// (checking for a 'Repository' file as an additional sanity check).
		// TODO: Remove all .cvsignore files!
		void recurse(String ruta) {
			DirectoryInfo dirs;
			FileInfo[] files;
			dirs = new DirectoryInfo(ruta);
		
			foreach (DirectoryInfo dir in dirs.GetDirectories()) {
				Console.WriteLine("Directory is : {0}", dir.FullName);
				this.lblRuta.Text = dir.FullName;
				
				if (dir.Name.Equals("CVS")) {
					if (dir.GetFiles("Repository").Length==1) {
						files = dir.GetFiles("*");
						foreach(FileInfo file in files) {
							file.Delete();
						}
					}
					dir.Delete();
				} else {
					recurse(dir.FullName);
				}
			}
		}
	}
}
